package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.NumberManagement;
import com.ruoyi.business.domain.dto.PowerStationFormDTO;
import com.ruoyi.business.domain.dto.PowerStationQueryDTO;
import com.ruoyi.business.domain.vo.PowerStationVO;
import com.ruoyi.business.mapper.ModelingManagementMapper;
import com.ruoyi.business.mapper.PowerStationMapper;
import com.ruoyi.business.service.IPowerStationService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电站管理Service业务层处理
 *
 * @author yangmeng
 * @date 2023-05-31
 */
@Service
public class PowerStationServiceImpl implements IPowerStationService {

    @Autowired
    private PowerStationMapper powerStationMapper;

    @Autowired
    private ModelingManagementMapper modelingManagementMapper;

    /**
     * 查询电站管理
     *
     * @param powerStationId 电站管理主键
     * @return 电站管理
     */
    @Override
    public PowerStationVO selectPowerStationByPowerStationId(Long powerStationId) {
        return powerStationMapper.selectPowerStationByPowerStationId(powerStationId);
    }

    /**
     * 查询电站管理列表
     *
     * @param powerStation 电站管理
     * @return 电站管理
     */
    @Override
    public List<PowerStationVO> selectPowerStationList(PowerStationQueryDTO powerStation) {
        return powerStationMapper.selectPowerStationList(powerStation);
    }

    /**
     * 新增电站管理
     *
     * @param powerStation 电站管理
     * @return 结果
     */
    @Override
    public int insertPowerStation(PowerStationFormDTO powerStation) {
        powerStation.setCreateTime(DateUtils.getNowDate());
        powerStation.setCreateBy(SecurityUtils.getUserId());
        return powerStationMapper.insertPowerStation(powerStation);
    }

    /**
     * 修改电站管理
     *
     * @param powerStation 电站管理
     * @return 结果
     */
    @Override
    public int updatePowerStation(PowerStationFormDTO powerStation) {
        powerStation.setUpdateTime(DateUtils.getNowDate());
        powerStation.setUpdateBy(SecurityUtils.getUserId());
        return powerStationMapper.updatePowerStation(powerStation);
    }

    /**
     * 批量删除电站管理
     *
     * @param powerStationIds 需要删除的电站管理主键
     * @return 结果
     */
    @Override
    public int deletePowerStationByPowerStationIds(Long[] powerStationIds) {
        for (Long powerStationId : powerStationIds) {
            if (modelingManagementMapper.countModelingManagementByPowerStationId(powerStationId) > 0) {
                PowerStationVO powerStationVO = powerStationMapper.selectPowerStationByPowerStationId(powerStationId);
                throw new ServiceException(String.format("%1$s已使用,不能删除", powerStationVO.getStationName()));
            }
        }
        return powerStationMapper.deletePowerStationByPowerStationIds(powerStationIds);
    }

    /**
     * 删除电站管理信息
     *
     * @param powerStationId 电站管理主键
     * @return 结果
     */
    @Override
    public int deletePowerStationByPowerStationId(Long powerStationId) {
        return powerStationMapper.deletePowerStationByPowerStationId(powerStationId);
    }

    /**
     * 校验电站名称是否唯一
     *
     * @param powerStation 电站管理信息
     * @return 结果
     */
    @Override
    public boolean checkNumberNameUnique(PowerStationFormDTO powerStation) {
        Long powerStationId = StringUtils.isNull(powerStation.getPowerStationId()) ? -1L : powerStation.getPowerStationId();
        PowerStationVO info = powerStationMapper.checkStationNameUnique(powerStation.getStationName());
        if (StringUtils.isNotNull(info) && info.getPowerStationId().longValue() != powerStationId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
